//
//  YXYBaseTableController.swift
//  SinaWeibo
//
//  Created by 袁向阳 on 16/6/23.
//  Copyright © 2016年 YXY.cn. All rights reserved.
//

import UIKit

class YXYBaseTableController: UITableViewController {

    // 创建访客视图
    lazy var visitorView:YXYVisitorView = YXYVisitorView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 通过一个颜色获得一张图片
        let image = drawImageFromColor(UIColor.redColor().colorWithAlphaComponent(1.0), size: kSize)
        navigationController?.navigationBar.setBackgroundImage(image, forBarMetrics: .Default)
    }
    
    // 通过一个颜色获得一张图片
    func drawImageFromColor(color: UIColor, size: CGSize) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        color.set()
        UIRectFill(CGRect(origin: CGPointZero, size: size))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    
    
    // 判断用户是否登录
    let isLogin:Bool = YXYUserAccountTool.sharedUserAccountTool.isLogin
    
    override func loadView() {
        
        if isLogin {
            
            super.loadView()
            
        }else {
            
            setUpNav()
            view = visitorView
            visitorView.visitorViewDelegate = self
            
        }
        
    }
    
    // 设置nav
    private func setUpNav () {
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "clickLoginBtn")
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: "clickLoginBtn")
    }
    

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 0
    }

    /*
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

        // Configure the cell...

        return cell

    }
    */
    
}

// MARK: - 访客视图的代理方法
extension YXYBaseTableController: YXYVisitorViewDelegate {
    func clickLoginBtn() {
        // 弹出授权登录控制器
        //print("弹出授权登录控制器")
        
        let OAuth = YXYOAuthController()
        let nav = UINavigationController(rootViewController: OAuth)
        presentViewController(nav, animated: true, completion: nil)
    }
    
}

// MARK: - tableView的代理方法
extension YXYBaseTableController {
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
    
    override func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let offsetY = scrollView.contentOffset.y
        
        if offsetY <= 0 {
            //print("\(offsetY)")
            let alpha = offsetY / -64.0
            setNavAlpha(alpha)
        }else {
            setNavAlpha(0.0)
        }
    }
    
    // 设置navigationBar的透明度
    func setNavAlpha (alpha: CGFloat) {
        //navigationController?.navigationBar.alpha = alpha
        
        let image = drawImageFromColor(UIColor.redColor().colorWithAlphaComponent(alpha), size: kSize)
        navigationController?.navigationBar.setBackgroundImage(image, forBarMetrics: .Default)
        navigationController?.navigationBar.shadowImage = UIImage()
    }
}
